﻿using System;
using System.Xml.Linq;
using System.Text;
using SedionFormDesigner.Serializer.BasicStruct;
using SedionFormDesigner.CusControls;
using System.Data;
using System.Collections.Generic;
using ValueHelper.Infrastructure;
using System.Text.RegularExpressions;

namespace SedionFormDesigner.Serializer
{
    public class SeriHtmlAndXml
    {
        private SeriHtmlAndXml() { }
        private static SeriHtmlAndXml instance = null;
        public static SeriHtmlAndXml Instance
        {
            get
            {
                if (instance == null)
                    instance = new SeriHtmlAndXml();
                return instance;
            }
        }

        public XmlFromModelList XmlToHtml(XElement xml)
        {
            XmlFromModelList modelList = new XmlFromModelList();
            foreach (XElement form in xml.Elements())
            {
                modelList.Add(xmlToHtml(form));
            }
            return modelList;
        }

        public XmlFromModelList XmlToHtmlReadonly(XElement xml, List<FormValueEntities> formValueEntity, DataTable dtspjg)
        {
            XmlFromModelList modelList = new XmlFromModelList();

            foreach (XElement form in xml.Elements())
            {

                modelList.Add(xmlToHtmlReadonly(form, formValueEntity));
            }
            //审批结果
            if (dtspjg != null && dtspjg.Rows.Count > 0)
            {
                var lastModel = modelList[modelList.GetCount() - 1];
                modelList[modelList.GetCount() - 1].AddRange(getHtml_Spjg(dtspjg, lastModel.MaxHeight));
            }
            return modelList;
        }



        private XmlFormModel xmlToHtml(XElement form)
        {
            var elems = form.Elements();
            XmlFormModel modelList = new XmlFormModel();
            var formWidth = form.Attribute("Width");
            if (formWidth != null)
            {
                modelList.Width = Convert.ToInt32(formWidth.Value);
                modelList.Height = Convert.ToInt32(form.Attribute("Height").Value);
                modelList.Top = Convert.ToInt32(form.Attribute("Top").Value);
                modelList.MaxHeight = form.Attribute("maxHeight") != null ? Convert.ToInt32(form.Attribute("maxHeight").Value) : 800; ;
            }

            foreach (XElement item in elems)
            {
                var type = item.Element("Type").Value;
                var name = item.Element("Name").Value;
                switch (type)
                {
                    case "BD_Label":
                        modelList.Add(name, getHtml_Label(item));
                        break;
                    case "BD_TextBox":
                        modelList.Add(name, getHtml_Text(item));
                        break;
                    case "BD_RichTextBox":
                        modelList.Add(name, getHtml_Textarea(item));
                        break;
                    case "BD_Button":
                        modelList.Add(name, getHtml_Button(item));
                        break;
                    case "BD_RadioButton":
                        modelList.Add(name, getHtml_RadioButton(item));
                        break;
                    case "BD_CheckBox":
                        modelList.Add(name, getHtml_Checkbox(item));
                        break;
                    case "BD_ComboBox":
                        modelList.Add(name, getHtml_Select(item));
                        break;
                    case "BD_PictureBox":
                        modelList.Add(name, getHtml_Image(item));
                        break;
                    case "BD_DateTimePicker":
                        modelList.Add(name, getHtml_Calender(item));
                        break;
                    case "BD_Currency":
                        modelList.Add(name, getHtml_Currency(item));
                        break;
                    case "BD_DataGridView":
                        modelList.Add(name, getHtml_Table(item));
                        break;
                    case "BD_Horizon":
                        modelList.Add(name, getHtml_Horizon(item));
                        break;
                    case "BD_Vertical":
                        modelList.Add(name, getHtml_Vertical(item));
                        break;
                    default:
                        break;
                }
            }
            return modelList;
        }

        private XmlFormModel xmlToHtmlReadonly(XElement form, List<FormValueEntities> formValueEntity)
        {
            var elems = form.Elements();
            XmlFormModel modelList = new XmlFormModel();
            var formWidth = form.Attribute("Width");
            modelList.MaxHeight = form.Attribute("maxHeight") != null ? Convert.ToInt32(form.Attribute("maxHeight").Value) : 800;
            if (formWidth != null)
            {
                modelList.Width = Convert.ToInt32(formWidth.Value);
                modelList.Height = Convert.ToInt32(form.Attribute("Height").Value);
                modelList.Top = Convert.ToInt32(form.Attribute("Top").Value);
            }

            foreach (XElement item in elems)
            {
                var type = item.Element("Type").Value;
                var name = item.Element("Name").Value;
                var value = getElementValue(name, formValueEntity);
                switch (type)
                {
                    case "BD_Label":
                        modelList.Add(name, getHtml_Label(item));
                        break;
                    case "BD_TextBox":
                        modelList.Add(name, getHtml_Text(item, value));
                        break;
                    case "BD_RichTextBox":
                        modelList.Add(name, getHtml_Textarea(item, value));
                        break;
                    case "BD_Button":
                        modelList.Add(name, getHtml_Button(item));
                        break;
                    case "BD_RadioButton":
                        modelList.Add(name, getHtml_RadioButton(item, value));
                        break;
                    case "BD_CheckBox":
                        modelList.Add(name, getHtml_Checkbox(item, value));
                        break;
                    case "BD_ComboBox":
                        modelList.Add(name, getHtml_Select(item, value));
                        break;
                    case "BD_PictureBox":
                        modelList.Add(name, getHtml_Image(item));
                        break;
                    case "BD_DateTimePicker":
                        modelList.Add(name, getHtml_Calender(item, value));
                        break;
                    case "BD_Currency":
                        modelList.Add(name, getHtml_Currency(item, value));
                        break;
                    case "BD_DataGridView":
                        modelList.Add(name, getHtml_Table(item, value));
                        break;
                    case "BD_Horizon":
                        modelList.Add(name, getHtml_Horizon(item));
                        break;
                    case "BD_Vertical":
                        modelList.Add(name, getHtml_Vertical(item));
                        break;
                    default:
                        break;
                }
            }
            return modelList;
        }
        private String getElementValue(String name, List<FormValueEntities> formValueEntity)
        {
            foreach (var item in formValueEntity)
            {
                if (item.fieldName == name)
                    return item.fieldValue;
            }
            return String.Empty;
        }

        String baseTemplate = "id='{0}'  name='{0}'";
        String styleTemplate = "left:{0}px; top:{1}px;width: {2}px;height:{3}px; position: absolute;font: {4}; font-size: {5}px;font-style:{6}; font-weight:{7};color:{8};";
        String labelTemplate = "<div {0} style='{1}' >{2}</div>";
        String textTemplate = "<input {0} class='txtInput txtunderline' type='text' style='{1}'  onfocus='limitlength(this); calculate(this);' value='{2}' />";
        String textareaTemplate = "<textarea {0} type='text' style='{1}' >{2}</textarea>";
        String buttonTemplate = "<input type='button' {0} style='{1}' value='{2}' />";
        String checkboxTemplate = "<div class='bdcheckbox' style='{1}'><input {0} style='margin:3px;' type='checkbox' onclick='checkvalue(this)' />{2}</div>";
        String radioTemplate = "<div  style='{1} '><input {0} type='radio' />{2}</div>";
        String selectTemplate = "<select {0} style='{1} padding-top:3px;'>{2}</select>";
        String imageTemplate = "<img {0} type='image' style='{1}' src='{2}'/>";
        String calenderTemplate = "<input {0} class='form_datepicker' type='text' style='{1}' value='{2}' readonly='readonly' />";
        String horizonTemplate = "<hr style='{1}' />";
        String verticalTemplate = "<div style='{1} border-left:1px solid black;'></div>";
        String tableTemplate = "<div iid='specialtable' class='bddivtable' style='{1} overflow:auto;'><input type='hidden' {0} class='tablevalue'/>{2}</div>";
        String treeviewEmployeeTemplate = "<div id='ztreeEmpoyee' class='ztree' {0} style='{1}'></div>";

        String selectOpertioTemplate = "<option  value='{0}'>{1}</option>";
        const String readonlyAttribute = "readonly='readonly'";

        String spjgTemplate = "<hr/ style='left: 30px; top: {4}px; width:550px;position: absolute;'>" +
                                "<div style='left: 40px; top: {5}px; width: 80px; height: 21px;position: absolute; font: 宋体; font-size: 12px; font-style: normal; font-weight: normal;text-align: right;'>{0}</div>" +//name：林稳
                                "<div style='left: 135px; top: {5}px; width: 38px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal; text-align: left;'>审核:</div>" +
                                "<div style='left: 183px; top: {5}px; width: 100px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal; text-align: left;'>{1}</div>" +//spjg:同意
                                "<div style='left: 329px; top: {5}px; width: 64px; height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal; text-align: left;'> 审核日期:</div>" +
                                "<div style='left: 399px; top: {5}px; width: 143px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal;text-align: left;'>{2}</div>" +//sprq:2012-12-04 12:12:12
                                " <div style='left: 135px; top: {6}px; width: 38px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal;text-align: left;'>意见:</div>" +
                                "<div style='left: 183px; top: {6}px; width: 359px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal; font-weight: normal;text-align: left;'>{3}</div>";

        ////string label = " <div id='{0}'  name='{0}' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}; text-align:[align]; '>{1}</div>";
        //String text = "<input id='{0}'  name='{0}' class = 'txtInput txtunderline' type='text' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-style:{8}; font-weight:{9};text-align:[align]; color:{10};background:{11}; ' [formular]  value='{1}' [onblur]  onfocus='limitlength(this); calculate(this);' {12} />";
        //String textarea = "<textarea id='{0}' class='bdtextarea'  name='{0}' type='text' style='resize: none; overflow:hidden; left:{2}px; top:{3}px;width:{4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11};' {12} >{1}</textarea>";
        //String button = "<input id='{0}' name='{0}' type='button'  style='left:{2}px; top:{3}px;width: {4}px;height:{5}px;  position: absolute;font: {6};text-align:[align]; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}; '  value='{1}'/>";
        //String checkbox = "<div class='bdcheckbox' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}; '><input id='{0}' name='{0}' style='margin:3px;' type='checkbox' replace onclick='checkvalue(this)' {13} />{1}</div>";
        //String radio = "<div  style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}; '><input id='{0}' name='{0}' type='radio' replace />{1}</div>";
        //String select = "<select id='{0}' name='{0}'  style='left:{2}px; top:{3}px;width: {4}px;height:25px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11};padding-top:3px;'>";
        //String selectValue = "<option  value='{0}'>{1}</option>";
        //String image = "<img id='{0}'  name='{0}' type='image' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-size: 14px;font-style:{8}; font-weight:{9};color:{10};background:{11}; ' src='replace'/>";
        //String calender = "<input id='{0}'  name='{0}' class='form_datepicker' type='text' style='left:{2}px; top:{3}px;width: 120px;height:{5}px; position: absolute;font: {6};font-style:{8}; font-weight:{9};color:{10};background:{11}; ' value='dataVal' readonly='readonly' />";
        //String horizon = "<hr style='left:{2}px; top:{3}px; width: {4}px; position: absolute;' />";
        //String vertical = "<div style='left:{2}px; top:{3}px; width: 1px; height:{5}px; position: absolute; border-left:1px solid black;'></div>";
        //String table = "<div class='bddivtable' style='left:{2}px; top:{3}px; position:absolute; height:{5}px; border:solid 1px black; overflow:auto;'>sqlBanding<div id='updateTable'>tableBanding</div></div>";
        //String treeviewEmployee = "<div id='ztreeEmpoyee' class='ztree' name='{0}' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;'></div>";

        private String getBaseValue(XElement item)
        {
            var objs = new object[]{ 
                item.Element("Name").Value,     // 0
                item.Element("Text").Value,     // 1   
            };

            return String.Format(baseTemplate, objs);
        }

        private String getStyleValue(XElement item)
        {
            var objs = new Object[]{              
                item.Element("Left").Value,     // 0         
                item.Element("Top").Value,      // 1         
                item.Element("Width").Value,    // 2         
                item.Element("Height").Value,   // 3         
                item.Element("FontName").Value, // 4         
                item.Element("FontSize").Value, // 5         
                item.Element("FontItalic").Value=="true"?"italic":"normal", // 6        
                item.Element("FontBold").Value=="true"?"bold":"normal",     // 7         
                item.Element("BackColor").Value // 8    
            };

            return String.Format(styleTemplate, objs);
        }

        private ElementModel getHtml_Label(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleValue = getStyleValue(item);
            styleValue = String.Concat(styleValue, " text-align:", item.Element("TextAlign").Value, "; ");

            var model = new ElementModel(labelTemplate, typeof(BD_Label));
            model.BaseValue = baseValue;
            model.StyleValue = styleValue;
            model.Value = item.Element("Text").Value;
            return model;
        }

        private ElementModel getHtml_Text(XElement item, String value)
        {
            var model = getHtml_Text(item);
            model.AddBaseValue(readonlyAttribute);
            model.Value = value;
            return model;
        }
        private ElementModel getHtml_Text(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleValue = getStyleValue(item);
            styleValue = String.Concat(styleValue, " text-align:", item.Element("TextAlign").Value, "; ");
            // 添加计算公式
            baseValue = String.Concat(baseValue, "formular='" + item.Element("Formular").Value + "'");

            var model = new ElementModel(textTemplate, typeof(BD_TextBox));
            model.BaseValue = baseValue;
            model.StyleValue = styleValue;
            model.Value = item.Element("AutoBind").Value;
            return model;
        }

        private ElementModel getHtml_Textarea(XElement item, String value)
        {
            var model = getHtml_Textarea(item);
            model.AddBaseValue(readonlyAttribute);
            model.Value = value;
            return model;
        }
        private ElementModel getHtml_Textarea(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleValue = getStyleValue(item);
            styleValue = String.Concat(styleValue, " resize: none; overflow:hidden;");

            var model = new ElementModel(textareaTemplate, typeof(BD_RichTextBox));
            model.BaseValue = baseValue;
            model.StyleValue = styleValue;
            model.Value = item.Element("Text").Value;
            return model;
        }

        private ElementModel getHtml_Button(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleValue = getStyleValue(item);
            styleValue = String.Concat(styleValue, " text-align:", item.Element("TextAlign").Value, "; ");

            var model = new ElementModel(buttonTemplate, typeof(BD_Button));
            model.BaseValue = baseValue;
            model.StyleValue = styleValue;
            model.Value = item.Element("Text").Value;
            return model;
        }

        private ElementModel getHtml_Checkbox(XElement item, String value)
        {
            var model = getHtml_Checkbox(item);
            model.AddBaseValue("disabled");
            if (value != null && value == "on")
                model.AddBaseValue("checked='checked'");
            return model;
        }
        private ElementModel getHtml_Checkbox(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleValue = getStyleValue(item);
            if (item.Element("Checked").Value == "true")
                baseValue = String.Concat(baseValue, " checked='checked'");

            var model = new ElementModel(checkboxTemplate, typeof(BD_CheckBox));
            model.BaseValue = baseValue;
            model.StyleValue = styleValue;
            model.Value = item.Element("Text").Value;
            return model;
        }

        private ElementModel getHtml_RadioButton(XElement item, String value)
        {
            var model = getHtml_RadioButton(item);
            model.AddBaseValue("disabled");
            if (value != null && value == "on")
                model.AddBaseValue("checked='checked'");
            return model;
        }
        private ElementModel getHtml_RadioButton(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleValue = getStyleValue(item);
            if (item.Element("Checked").Value == "true")
                baseValue = String.Concat(baseValue, " checked='checked'");

            var model = new ElementModel(radioTemplate, typeof(BD_RadioButton));
            model.BaseValue = baseValue;
            model.StyleValue = styleValue;
            model.Value = item.Element("Text").Value;

            return model;
        }

        private ElementModel getHtml_Select(XElement item, String value)
        {
            var baseValue = getBaseValue(item);
            var styleVaue = getStyleValue(item);

            var model = new ElementModel(selectTemplate, typeof(BD_ComboBox));
            model.BaseValue = baseValue;
            model.StyleValue = styleVaue;
            model.Value = String.Empty;
            model.AddBaseValue(readonlyAttribute);
            model.Value = ProduSelectOption(value);
            return model;
        }
        private ElementModel getHtml_Select(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleVaue = getStyleValue(item);

            var model = new ElementModel(selectTemplate, typeof(BD_ComboBox));
            model.BaseValue = baseValue;
            model.StyleValue = styleVaue;
            if (item.Element("AutoBind").Value == "不绑定")
                model.Value = ProduSelectOption(item.Element("SQL").Value);
            else
            {
                model.Value = item.Element("SQL").Value;
                model.ValueType = "SQL";
            }
            return model;
        }

        public String ProduSelectOption(String option)
        {
            if (option == String.Empty)
                return String.Empty;

            var result = String.Empty;
            var ops = option.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var op in ops)
            {
                var kv = op.Split(',');
                if (kv.Length > 1)
                    result = String.Concat(result, String.Format(selectOpertioTemplate, kv[0], kv[1]));
                else
                    result = String.Concat(result, String.Format(selectOpertioTemplate, kv[0], kv[0]));
            }
            return result;
        }

        private ElementModel getHtml_Image(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleValue = getStyleValue(item);
            String value = String.Concat("../../UploadFiles/Form/", item.Element("Image").Value);

            var model = new ElementModel(imageTemplate, typeof(BD_PictureBox));
            model.BaseValue = baseValue;
            model.StyleValue = styleValue;
            model.Value = value;
            return model;
        }

        private ElementModel getHtml_Calender(XElement item, String value)
        {
            var model = getHtml_Calender(item);
            model.AddBaseValue(readonlyAttribute);
            model.Value = value;

            return model;
        }
        private ElementModel getHtml_Calender(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleValue = getStyleValue(item);
            var rq = @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
            var sj = @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$";
            Regex re_rq = new Regex(rq);
            var tmptemplate = string.Empty;
            if (re_rq.IsMatch(item.Element("TimeValue").Value))
            {
                tmptemplate = calenderTemplate.Replace("form_datepicker", "form_datepicker_rq");
            }
            else
            {
                tmptemplate = calenderTemplate.Replace("form_datepicker", "form_datepicker_sj");
            }
            var model = new ElementModel(tmptemplate, typeof(BD_DateTimePicker));
            model.BaseValue = baseValue;
            model.StyleValue = styleValue;
            model.Value = item.Element("TimeValue").Value;
            return model;
        }

        private ElementModel getHtml_Table(XElement item, String value)
        {
            var styleValue = getStyleValue(item);
            var model = new ElementModel(tableTemplate, typeof(BD_DataGridView));
            model.StyleValue = styleValue;
            var values = value.Split(';');
            var titles = values[0].Split(',');
            var headTemplate = "<thead>{0}</thead>";
            var column = String.Empty;
            foreach (String title in titles)
            {
                var tl = title.Split('-');
                column = String.Concat(column, String.Format(TableHelper.thTemplate, title, tl[0]));
            }
            column = String.Format(headTemplate, column);
            var bodyTemplate = "<tbody>{0}</tbody>";
            var row = String.Empty;
            for (int i = 1; i < values.Length; i++)
            {
                var vals = values[i].Split(',');

                foreach (var val in vals)
                {
                    row = String.Concat(row, String.Format(TableHelper.tdlableTemplate, val));
                }
                row = String.Concat("<tr>", row, "</tr>");
            }
            row = String.Format(bodyTemplate, row);
            model.Value = String.Format("<table class='bdtable'>{0}{1}</table>", column, row);

            return model;
        }
        private ElementModel getHtml_Table(XElement item)
        {
            var styleValue = getStyleValue(item);
            var baseValue = getBaseValue(item);

            var model = new ElementModel(tableTemplate, typeof(BD_DataGridView));
            model.StyleValue = styleValue;
            model.BaseValue = baseValue;

            if (item.Element("SQL") == null)
            {
                var cols = item.Element("Columns").Value;
                var rows = item.Element("Rows").Value;
                var addable = item.Element("TableType").Value == "Addable";
                if (addable)
                    model.Value = ProduAddableTable(cols, rows);
                else
                    model.Value = ProduTable(cols, rows);
            }
            else
            {
                model.ValueType = "SQL";
                model.Value = item.Element("SQL").Value;
            }

            return model;
        }

        private List<Keyval<string, ElementModel>> getHtml_Spjg(DataTable dt_spjg, int maxHeight)
        {
            List<Keyval<string, ElementModel>> list = new List<Keyval<string, ElementModel>>();
            for (int i = 0; i < dt_spjg.Rows.Count; i++)
            {
                SpjgElement spjgElement = new SpjgElement(spjgTemplate, dt_spjg.Rows[i]["审批人"].ToString(), dt_spjg.Rows[i]["部门"].ToString(), dt_spjg.Rows[i]["职位"].ToString(),
                            dt_spjg.Rows[i]["审批意见"].ToString(), dt_spjg.Rows[i]["审批时间"].ToString(), dt_spjg.Rows[i]["审批结果"].ToString(), maxHeight + i * 70);

                list.Add(new Keyval<string, ElementModel>() { Key = "spjg" + i.ToString(), Value = spjgElement });
            }

            return list;

        }

        #region Addable Table

        public String ProduAddableTable(String columns, String rows)
        {
            if (columns == String.Empty)
                return String.Empty;

            var headTemplate = "<thead>{0}</thead>";
            var cos = columns.Split(';');
            var column = "<th></th>";
            var gatherValue = "";
            for (int i = 0; i < cos.Length; i++)
            {
                var cols = cos[i].Split('-');
                column = String.Concat(column, String.Format(TableHelper.thTemplate, (cols[0] + '-' + cols[1]), cols[0]));
                if (cols[3] != "None")
                    gatherValue += i + "-" + cols[3] + ";";
            }
            var operCol = "<th>";
            operCol = String.Concat(operCol, TableHelper.TbHyPerLink("<img src='../../images/icons/plus.png'  title='添加一行' style='padding:0 10px' />", "tbaddrow(this)"),
                TableHelper.TbHyPerLink("<img src='../../images/icons/edit.png' title='合计' style='padding:0 10px' />", "tbgather(this)"), "</th>");
            column = String.Concat(column, operCol);
            column = String.Format(headTemplate, column);
            var result = "<table class='addable bdtable'>{0}</table>";
            result = String.Format(result, String.Concat(column, produAddableTableBody(rows)));
            result = String.Concat(result, "<input id='tbgather' type='hidden' value='" + gatherValue + "' />");
            return result;
        }

        private String produAddableTableBody(String rows)
        {
            var bodyTemplate = "<tbody>{0}</tbody>";
            if (rows == String.Empty)
                return String.Format(bodyTemplate, String.Empty);

            var result = String.Empty;
            var rws = rows.Split(';');
            foreach (var rw in rws)
            {
                var r = rw.Split(',');
                for (int i = 0; i < r.Length; i++)
                {
                    var row = r[i].Split('-');
                    Boolean @readonly = true;
                    if (row.Length == 1 || row[1] == "False")
                        @readonly = false;
                    if (@readonly)
                        result = String.Concat(result, String.Format(TableHelper.tdlabelAddableTemplate(i), row[0]));
                    else
                        result = String.Concat(result, String.Format(TableHelper.tdtextAddableTemplate(i), row[0]));
                }
                result = String.Concat(result, "<td>", TableHelper.TbHyPerLink("删除", "tbdelrow(this)"), "</td>");
                result = String.Concat("<tr>", result, "</tr>");
            }
            return String.Format(bodyTemplate, result);
        }

        #endregion

        #region Normal Table

        public String ProduTable(DataTable dt)
        {
            var rows = dt.Rows;
            var cols = dt.Columns;
            var result = String.Empty;
            for (int i = 0; i < cols.Count; i++)
            {
                result = String.Concat(result, String.Format(TableHelper.thTemplate, (cols[i].ColumnName + '-' + cols[i].Caption), cols[i].ColumnName));
            }
            var headTemplate = "<thead>{0}</thead>";
            var headResult = String.Format(headTemplate, result);

            for (int i = 0; i < rows.Count; i++)
            {
                for (int j = 0; j < cols.Count; j++)
                {
                    result = String.Concat(result, String.Format(TableHelper.tdlableTemplate, rows[i][j]));
                }
            }
            var bodyTemplate = "<tbody>{0}</tbody>";
            var bodyResult = String.Format(bodyTemplate, result);
            var tableTemplate = "<table class='bdtable'>{0}</table>";
            return String.Format(tableTemplate, String.Concat(headResult, bodyResult));
        }

        private String ProduTable(String columns, String rows)
        {
            if (columns == String.Empty)
                return String.Empty;

            var headTemplate = "<thead>{0}</thead>";
            var cos = columns.Split(';');
            var column = String.Empty;
            foreach (var col in cos)
            {
                var cols = col.Split('-');
                column = String.Concat(column, String.Format(TableHelper.thTemplate, (cols[0] + '-' + cols[1]), cols[0]));
            }
            column = String.Format(headTemplate, column);
            var result = "<table class='bdtable'>{0}</table>";
            result = String.Format(result, String.Concat(column, ProduTableBody(rows)));

            return result;
        }

        public String ProduTableBody(String rows)
        {
            var bodyTemplate = "<tbody>{0}</tbody>";
            if (rows == String.Empty)
                return String.Format(bodyTemplate, rows);

            var result = String.Empty;
            var rws = rows.Split(';');
            foreach (var rw in rws)
            {
                var r = rw.Split(',');
                for (int i = 0; i < r.Length; i++)
                {
                    var row = r[i].Split('-');
                    Boolean @readonly = true;
                    if (row.Length == 1 || row[1] == "False")
                        @readonly = false;
                    if (@readonly)
                        result = String.Concat(result, String.Format(TableHelper.tdlableTemplate, row[0]));
                    else
                        result = String.Concat(result, String.Format(TableHelper.tdtextTemplate, row[0]));
                }
                result = String.Concat("<tr>", result, "</tr>");
            }
            return String.Format(bodyTemplate, result);
        }

        #endregion

        private ElementModel getHtml_Currency(XElement item, String value)
        {
            var model = getHtml_Currency(item);
            model.AddBaseValue(readonlyAttribute);
            model.Value = value;
            return model;
        }
        private ElementModel getHtml_Currency(XElement item)
        {
            var baseValue = getBaseValue(item);
            var styleValue = getStyleValue(item);
            // 添加RMB换算
            if (item.Element("CurrencyType").Value == "RMB")
                baseValue = String.Concat(baseValue, "onblur='getCapitalNumber(this);'");
            styleValue = String.Concat(styleValue, " text-align:", item.Element("TextAlign").Value, "; ");

            var model = new ElementModel(textTemplate, typeof(BD_Currency));
            model.BaseValue = baseValue;
            model.StyleValue = styleValue;
            model.Value = item.Element("Text").Value;

            return model;
        }

        private ElementModel getHtml_Horizon(XElement item)
        {
            var styleValue = getStyleValue(item);

            var model = new ElementModel(horizonTemplate, typeof(BD_Horizon));
            model.StyleValue = styleValue;
            return model;
        }

        private ElementModel getHtml_Vertical(XElement item)
        {
            var styleValue = getStyleValue(item);

            var model = new ElementModel(verticalTemplate, typeof(BD_Vertical));
            model.StyleValue = styleValue;
            return model;
        }

        private ElementModel getHtml_TreeviewEmployee(XElement item)
        {
            var styleValue = getStyleValue(item);
            var model = new ElementModel(treeviewEmployeeTemplate, typeof(BD_TreeView_Employee));
            model.StyleValue = styleValue;
            model.BaseValue = item.Element("Text").Value;
            return model;
        }

        private class TableHelper
        {
            private const String hiddenTemplate = "<input type='hidden' class='tbvalget' value='{0}'/>";
            public const String thTemplate = "<th align='center'><lable>{1}</lable>" + hiddenTemplate + "</th>";
            public const String tdlableTemplate = "<td><input type='text' readonly='readony' class='txtunderline' value='{0}'>" + hiddenTemplate + "</td>";
            public const String tdtextTemplate = "<td><input type='text' class='txtunderline txtTable' value='{0}' >" + hiddenTemplate + "</td>";

            public static String tdlabelAddableTemplate(Int32 index)
            {
                return "<td index='" + index + "'><input type='text' readonly='readony' class='txtunderline' value='{0}'>" + hiddenTemplate + "</td>";
            }

            public static String tdtextAddableTemplate(Int32 index)
            {
                return "<td index='" + index + "'><input type='text' class='txtunderline txtTable' value='{0}' >" + hiddenTemplate + "</td>";
            }

            public static String TbHyPerLink(String text, String @event)
            {
                return "<a onclick='" + @event + "' style='color: black; text-underline-position: below; cursor: pointer'>" + text + "</a>";
            }
        }

    }
}
